﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.gates {
    import Engine.Managers.*;
    import flash.display.*;
    import Display.DialogUI.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Display.*;
    import Display.UpgradesUI.*;
    import Display.GateUI.*;

    public class InventoryGate extends AbstractGate {

        private var m_askDialog:AskFriendsDialog;
        protected var m_customViralType:String;

        public function InventoryGate(_arg1:String){
            super(_arg1);
        }
        override public function checkForKeys():Boolean{
            var _local1:String;
            super.checkForKeys();
            for (_local1 in m_keys) {
                if (Global.player.inventory.getItemCountByName(_local1) < m_keys[_local1]){
                    return (false);
                };
            };
            return (true);
        }
        public function checkWithAdditionalKey(_arg1:Item):Boolean{
            var _local2:String;
            var _local3:int;
            for (_local2 in m_keys) {
                _local3 = ((_local2)==_arg1.name) ? 1 : 0;
                if ((Global.player.inventory.getItemCountByName(_local2) + _local3) < m_keys[_local2]){
                    return (false);
                };
            };
            return (true);
        }
        override public function getRequirementString():String{
            var _local2:String;
            var _local1 = "";
            for (_local2 in m_keys) {
                if (m_keys[_local2] == 0){
                } else {
                    _local1 = (_local1 + (" " + ZLoc.t("Dialogs", "GateKey", {
                        qty:m_keys[_local2],
                        item:Global.gameSettings.getItemByName(_local2).localizedName
                    })));
                };
            };
            _local1 = _local1.slice(0, (_local1.length - 1));
            return (ZLoc.t("Dialogs", "GateRequirementString", {items:_local1}));
        }
        public function buyKey(_arg1:Item):Boolean{
            var _local2:GateDialog;
            var _local3:MunInventoryUpgradesDialog;
            if (Global.player.canBuyCash(_arg1.cash, true)){
                GameTransactionManager.addTransaction(new TBuyItem(_arg1.name, 1, m_unlockItemName));
                if (this.checkForKeys() == true){
                    _local2 = (m_dialog as GateDialog);
                    if (_local2){
                        _local2.activateFinishButton();
                    };
                    _local3 = (m_dialog as MunInventoryUpgradesDialog);
                    if (_local3){
                        _local3.activateFinishButton();
                    };
                };
                updateItemArrowState();
                return (true);
            };
            if (m_dialog){
                m_dialog.close();
                m_dialog = null;
            };
            return (false);
        }
        override public function displayGate(_arg1:String=null, _arg2:String=null):void{
            var _local4:String;
            var _local5:Item;
            var _local6:String;
            var _local7:Object;
            this.m_customViralType = _arg1;
            m_dialog = null;
            var _local3:Array = new Array();
            for (_local4 in m_keys) {
                _local7 = new Object();
                _local7.item = Global.gameSettings.getItemByName(_local4);
                _local7.amountNeeded = m_keys[_local4];
                _local7.buyCallback = this.buyKey;
                _local7.askCallback = this.askForKey;
                _local3.push(_local7);
            };
            _local7 = new Object();
            _local7.item = Global.gameSettings.getItemByName(SEND_GIFT_ITEM);
            _local7.amountNeeded = 0;
            _local3.push(_local7);
            m_cellFactory = new KeyCellItemFactory(_local3);
            _local5 = Global.gameSettings.getItemByName(m_unlockItemName);
            _local6 = _arg2;
            if (_local6 == null){
                _local6 = ZLoc.t("Dialogs", "GateDialogTitle", {item:_local5.localizedName});
            };
            m_dialog = new GateDialog(_local3, m_cellFactory, _local6, this.getRequirementString(), this.checkForKeys, unlockGate, buyAll);
            (m_dialog as GateDialog).setGateType(m_name);
            StatsManager.milestone(("see_gate_for_" + m_unlockItemName));
            UI.displayPopup(m_dialog);
        }
        override protected function takeKeys():void{
            var _local1:String;
            for (_local1 in m_keys) {
                Global.player.inventory.removeItems(_local1, m_keys[_local1]);
            };
        }
        protected function askForKey(_arg1:Item, _arg2:DisplayObject):void{
            var _local3:Boolean;
            var _local5:String;
            var _local6:GenericDialog;
            if (m_dialog){
                m_dialog.close();
                m_dialog = null;
            };
            var _local4:String = m_virals[_arg1.name];
            if ((((_local4 == AskFriendsDialog.REQUEST_BUILDABLE)) || ((_local4 == "")))){
                if (this.m_customViralType){
                    _local3 = Global.world.viralMgr.sendCustomMaterialRequest(this.m_customViralType, Global.player, Global.gameSettings.getItemByName(m_unlockItemName).localizedName, _arg1);
                } else {
                    _local3 = Global.world.viralMgr.sendMunicipalMaterialRequest(Global.player, Global.gameSettings.getItemByName(m_unlockItemName).localizedName, _arg1);
                };
            } else {
                if (_local4 == AskFriendsDialog.FEED_BUILDABLE){
                    if (this.m_customViralType){
                        _local3 = Global.world.viralMgr.sendCustomMaterialFeed(this.m_customViralType, Global.player, Global.gameSettings.getItemByName(m_unlockItemName).localizedName, _arg1);
                    } else {
                        _local3 = Global.world.viralMgr.sendMunicipalMaterialFeed(Global.player, Global.gameSettings.getItemByName(m_unlockItemName).localizedName, _arg1);
                    };
                };
            };
            if (!_local3){
                _local5 = ZLoc.t("Dialogs", "MunicipalThrottlingMessage");
                _local6 = new GenericDialog(_local5);
                UI.displayPopup(_local6);
            };
        }

    }
}//package Classes.gates 
